const Router = require("koa-router");
const routes = new Router();
const mysql = require("mysql");

const connection = mysql.createConnection({
    host: "localhost",
    user: "app",
    password: "111111",
    database: "app"
})
connection.connect();
//注册接口
routes.post("/api/regist", async (ctx, next) => {
    try {
        const { phone, pass, age, email, idCard, chnName, enName } = ctx.request.body.user;
        console.log(phone, pass, age, email, idCard, chnName, enName)
        const res = await new Promise((resolve, reject) => {
            connection.query("insert into user set ?", { phone, pass, age, email, idCard, chnName, enName }, function (err, result) {
                if (err) reject(err.message)
                resolve(result)
            })
        })
        ctx.response.body = {
            code: 1,
            msg: "注册成功"
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: error
        }
    }
})
//登录接口
routes.post("/api/login", async (ctx, next) => {
    /* const { pass, phone } = ctx.request.body.user;
    console.log(pass, phone) */
    try {
        const { pass, phone } = ctx.request.body.user;
        const res = await new Promise((resolve, reject) => {
            connection.query("select * from user where phone=? and pass=?", [phone, pass], function (error, result) {
                if (error) reject(error.message)
                resolve(result)
            })
        })
        if (res.length === 1) {
            ctx.response.body = {
                code: 1,
                msg: "登录成功"
            }
        } else {
            ctx.response.body = {
                code: 0,
                msg: "账号或密码错误"
            }
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: "登录失败"
        }
    }
});
//签到接口
routes.post("/api/signin", async (ctx, next) => {
    const { phone } = ctx.request.body;
    const nowTime = new Date().toLocaleDateString()
    /* console.log(phone) */
    const res = await new Promise((resolve, reject) => {
        connection.query("select * from user where phone=?", phone, function (error, result) {
            if (error) reject(error.message)
            resolve(result)
        })
    })
    if (res.length > 0) {
        if (res[0].signinDate === nowTime) {
            ctx.response.body = {
                code: 0,
                msg: "已经签到"
            }
        } else {
            await new Promise((resolve, reject) => {
                connection.query("update user set signinDate=?,signinNum=? where phone=?", [nowTime, res[0].signinNum + 1, phone], function (error, result) {
                    if (error) reject(error)
                    resolve(result)
                })
            })
            ctx.response.body = {
                code: 1,
                data: res[0].signinNum + 1,
                msg: "签到成功"
            }
        }
    } else {
        ctx.response.body = {
            code: 0,
            msg: "请先登录"
        }
    }
    ctx.response.body = {
        code: 1,
        data: res
    }
})

module.exports = routes




